<?php include("../includes/header-member.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">
                <div id="sidebar">                 
                    <?php
					// include the corresponding navigation pages
                    include("shopSidebar.php");
                    include("../../includes/navigation.php");
					?>     
                </div>                     
            <?php
                    // we need to get the latest products incase any prices have changed and update our prices accordingly by the margin
                    require("reloadProducts.php");
                    reloadProducts();
             ?>
            <!-- main body -->
            <h2> Ye Olde Swim Shoppe </h2>
            
            <br><br>             
            Welcome to the Lazy Swim Equipment Store. Here you can find all your swimming requirements. Plus, if you purchase items from our
            store you will match the rest of the team. 
            Below are some recommendations, but you can also search for items, or look at items by category using the sidebar.
            <br><br>
            
            <h3> Recommended Items </h3>
            
            <?php
                $user = $_SESSION['ls_userid'];
                $sql = mysql_query("SELECT * FROM orders where customerID = '$user' ORDER BY orderTime DESC") or die(mysql_error());
                $orderCount = mysql_num_rows($sql);
                
                //if they have ordered anything display some random items from
                // the same category or size of the last order
                if( $orderCount > 0 ) {
                   $order =  mysql_fetch_assoc($sql);
                   $sql =  mysql_query("SELECT * from itemsOrdered where orderID = '$order[orderID]'");
                   
                   $categories = array();
                   while( $row = mysql_fetch_assoc($sql) ) {
                        $prod_sql = mysql_query("SELECT * from products where productID = '$row[productID]'");
                        while($product = mysql_fetch_assoc($prod_sql)) {
                            array_push($categories, $product[category]);
                        }
                   }                  

                   // for each category add this as an option in the sql
                   $sql = "SELECT * FROM products WHERE ";
                   $counter = sizeof($categories);
                   foreach ( $categories AS $c ) {
                        $sql .= "category = '$c' ";
                        // don't display an OR if it is the last item
                        --$counter;
                        if( $counter != 0 ) {
                            $sql .= "OR ";
                        }
                   }
                   $sql .= " ORDER BY rand() limit 10";   
                   $result = mysql_query($sql);
                }
                // otherwise, display random items
                else{
                     $result = mysql_query("SELECT * FROM products ORDER BY rand() limit 10");
                }
            ?>
             <br><table width="65%" cellpadding="5" cellspacing="5" class="text"> 
                <tr align="center" id="listTableHeader"> 
                    <th>Product Name</th> 
                    <th>Price</th> 
                    <th>Size</th> 
                    <th>Category</th>
                </tr> 
            <?php                        
                $i = 0;
                while($row = mysql_fetch_assoc($result)) { 
                    // this is just to make the table pretty with alternating tr colours
                    if($i % 2) { 
                        $class = 'row1'; 
                    } 
                    else { 
                        $class = 'row2'; 
                    } 
                    $i++;
                    ?>
                    <tr class="<?php echo $class; ?>"> 
                        <td align="center"><a href="viewProductDetails.php?name=<?php echo $row[name]; ?>"><?php echo $row[name]; ?></a></td> 
                        <td align="center">$<?php echo $row[price]; ?></td> 
                        <td align="center"><?php echo $row[size]; ?></td>
                        <td align="center"><?php echo $row[category]; ?></td>
                    </tr> 
                      <?php                    
                    } // end of while
                    ?>
                    <tr> 
                        <td colspan="5" align="center"> 
                            <?php echo $pagingLink; ?>
                        </td> 
                     </tr> 
                </table>
            <!-- end of main -->
            
            <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>